#include "labelx.h"
#include <QDebug>
#include <QMouseEvent>

LabelX::LabelX(QWidget *parent): QLabel{parent}
{
    // 设置鼠标跟踪
    this->setMouseTracking(true);
}

static int count = 0;

void LabelX::enterEvent(QEvent *event)
{
    QLabel::enterEvent(event);
    this->setText(QString::number(count++));
}

void LabelX::leaveEvent(QEvent *event)
{
    Q_UNUSED(event);
    this->setText(QString::number(count++));
}

// 鼠标按下
void LabelX::mousePressEvent(QMouseEvent* ev)
{
    // qDebug() << "mousePressEvent: " << ev->button() << ev->pos() << ev->globalPos();
    if(ev->button() == Qt::LeftButton)
    {
        qDebug() << "鼠标按下: " << "X=" << ev->x() << "Y=" << ev->y();
    }
}

// 鼠标移动
void LabelX::mouseMoveEvent(QMouseEvent* ev)
{
    if(ev->buttons() & Qt::LeftButton)
    {
        qDebug() << "鼠标移动中: " << "X=" << ev->x() << "Y=" << ev->y();
    }
}

// 鼠标释放
void LabelX::mouseReleaseEvent(QMouseEvent *ev)
{
    // qDebug() << "mouseReleaseEvent: " << ev->button() << ev->pos() << ev->globalPos();
    if(ev->button() == Qt::LeftButton)
    {
        qDebug() << "鼠标释放: " << "X=" << ev->x() << "Y=" << ev->y();
    }
}
